﻿/*
--------------------------------------
File: CropLicenseplate.cs
Project: Themaopdracht 7
Author: Maarten Kroon
Description:
A algorithm which crops the image around the license plate and returns the original colors
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
   public class CropLicenseplateColoured: ColorInterface 
    {
       /// <summary>
        /// The algorithm which crops the image around the license plate and returns the original colors
       /// </summary>
       /// <param name="blackWhiteSourceArray">The monochrome array that was used in simpleColorDetectMonochrome</param>
       /// <param name="colorSourceArray">The original image array</param>
       /// <param name="width">The image width</param>
       /// <returns>A bitmap cropped around the license plate with the original image colors</returns>
        public Bitmap DoAlgorithm(ref int[] blackWhiteSourceArray, ref int[] colorSourceArray, int width)
		{
            //Finds the dimensions for the new bitmap
            int firstX = width, firstY = 0, lastX = 0, lastY = 0;

            for (int i = 0; i < blackWhiteSourceArray.Length; i++)
            {
                if (blackWhiteSourceArray[i] != BLACK)
                {
                    //The first Y is the first non black pixel
                    if (firstY == 0)
                    {
                        firstY = i / width;
                    }
                    //The first X is the most left non black pixel
                    if (firstX > (i % width))
                    {
                        firstX = i % width;
                    }
                    //The last X pixel is the most right non black pixel
                    if ((i % width) > (lastX % width))
                    {
                        lastX = i % width;
                    }
                    //The last Y pixel is the last right non pixel
                    lastY = i / width;
                }
            }

            if (lastX != 0 && lastY != 0 && firstY != 0)
            {
                //Creates the new bitmap with the dimensions and the new array
                Bitmap croppedBitmap = new Bitmap(lastX - firstX, lastY - firstY);
                int[] croppedArray = MyImage.BmpToArray(croppedBitmap);

                //Crops the image
                int counter = 0;
                for (int y = firstY; y < lastY; y++)
                {
                    for (int x = firstX; x < lastX; x++)
                    {
                        croppedArray[counter] = colorSourceArray[y * width + x];
                        counter++;
                    }
                }
                croppedBitmap = MyImage.ArrayToBmp(croppedArray, croppedBitmap);
                return croppedBitmap;
            }
            else
            {
                return null;
            }            
        }
    }
}


